send "lock" message upon entering critical section
send "unlock" message to leave critical section
There are classes with different implementations and performance characteristics. There are example usages preceeding each class definition.
Use NSLock to protect regions of code that can consume long periods of time (disk I/O, heavy computation).
Use NSConditionLock for those cases where you wish only certain threads to awaken based on some condition you define, or for those cases when you have both short and long critical sections.
Use NSRecursiveLock to protect regions of code or data that may be accessed by the same thread. The lock will not block if it is already held by the same thread. This does not provide mutual exclusion with naive signal handlers...
*/
@protocol NSLocking
- (void)lock;
// acquire lock (enter critical section)
- (void)unlock;
// release lock (leave critical section)
// In order to enable clients to only have locks when processes become multithreaded, it must be possible to unlock a lock freshly created (i.e. that has not been locked).
@end
/*************** NSLock ***************/
/* NSLock:
Ordinary, garden variety lock. Use this if in doubt.
Best protection for critical sections that do system calls or heavy computation.
Threads will sleep if they cannot immediately acquire the lock